package com.trf.service.user;

import com.trf.dao.BaseDao;
import com.trf.dao.user.UserDao;
import com.trf.dao.user.UserDaoIml;
import com.trf.pojo.User;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author 唐荣锋
 * @Date 2022/3/9-星期三-17:27
 */
public class UserServiceImpl implements UserService{

    //业务层都调用dao层，引用dao层
    private UserDao userDao;
    public UserServiceImpl(){
         userDao = new UserDaoIml();
    }

    //登录
    public User login(String userCode, String password) {
        Connection connection = null;
        User user = null;

        try {
            connection = BaseDao.getConnection();
            //通过业务层调用对应的具体的数据库操作
            user = userDao.getLoginUser(connection,userCode);

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }

        //匹配密码
        if (user!=null){
            if (!user.getUserPassword().equals(password))
                user=null;
        }
        return user;
    }


    //修改密码
    public boolean updatePwd(int id, String pwd) {
//       测试节点  iSystem.out.println("UserServlet："+pwd);
       //获得数据库连接
        Connection connection = null;
        boolean flag = false;
        //修改密码
        try {
            connection = BaseDao.getConnection();
            if (userDao.updatePwd(connection,id,pwd)>0){
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //关闭资源
            BaseDao.closeResource(connection,null,null);
        }
        //修改成功返回true,否则返回false
        return flag;
    }

    //查询记录数
    public int getUserCount(String username, int userRole) {
        Connection connection = null;
        int count = 0;

        try {
            connection = BaseDao.getConnection();
            count = userDao.getUserCount(connection,username,userRole);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return count;
    }

    public List<User> getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize) {
        Connection connection = null;
        List<User> userList = null;
        System.out.println("queryUserName----"+queryUserName);
        System.out.println("queryUserRole----"+queryUserRole);
        System.out.println("currentPageNo----"+currentPageNo);
        System.out.println("pageSize-----"+pageSize);
        try {
            connection = BaseDao.getConnection();
            userList = userDao.getUserList(connection,queryUserName,queryUserRole,currentPageNo,pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return userList;
    }

//    @Test 测试业务层已经数据库连接调用等
//    public void test(){
//        UserServiceImpl userService = new UserServiceImpl();
//        User admin = userService.login("admin", "1234er456");
//        System.out.println(admin.getUserPassword());
//    }

//    @Test 调试已写程序
//    public void test(){
//        UserServiceImpl userService = new UserServiceImpl();
//        int userCount = userService.getUserCount(null, 0);
//        System.out.println(userCount);
//    }

   /* @Test
    public void test(){
        UserServiceImpl userService = new UserServiceImpl();
        userService.getUserList()
    }*/
}
